package cc.customer.system.service.impl;

import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.Utils;
import cc.customer.system.dao.SaleTagMapper;
import cc.customer.system.domain.SaleTag;
import cc.customer.system.service.SaleTagService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaleTagServiceImpl extends BaseService<SaleTag> implements SaleTagService {

   private static final Logger log = LoggerFactory.getLogger(SaleTagServiceImpl.class);

   @Autowired
   private SaleTagMapper saleTagDao;

   @Override
   public int save(SaleTag saleTag){
      saleTag.setTagGroupId("0");
      String uuid = Utils.getUUID();
      saleTag.setTagUuids(uuid);
      saleTag.setTagSelect("0");
      saleTag.setTagStatus("1");
      return super.save(saleTag);
   }

   @Override
   public List<SaleTag> selectByTagName(SaleTag saleTag) {
      Example example = new Example(SaleTag.class);
      example.createCriteria().andCondition("tag_group_id=",saleTag.getTagGroupId()).andCondition("tag_name=",saleTag.getTagName()).andEqualTo("tagStatus","1");
      return super.selectByExample(example);
   }

   @Override
   public SaleTag selectByGroupName(SaleTag saleTag){
      Example example = new Example(SaleTag.class);
      example.createCriteria().andCondition("tagGroupName=",saleTag.getTagGroupName()).andEqualTo("tagStatus","1");
      SaleTag sale = super.selectByKey(saleTag);
      return sale;
   }

   /**初始化分组*/
   @Override
   public Map<String, Object> selectTagGroup() {
      Example example = new Example(SaleTag.class);
      example.createCriteria().andCondition("tag_group_id=","0").andEqualTo("tagStatus","1");
      List<SaleTag> list = this.selectByExample(example);
      List<SaleTag> list1 = new ArrayList<SaleTag>();
      Map<String,Object> map = new HashMap<>();
      if(list!=null){
         for(int i = 0;i<list.size();i++){
            String groupid = list.get(0).getTagUuid();
            Example example1 = new Example(SaleTag.class);
            example1.createCriteria().andCondition("tag_group_id=",groupid).andEqualTo("tagStatus","1");
            list1 = this.selectByExample(example1);
         }
      }
      map.put("group",list);
      map.put("tag",list1);
      return map;
   }

   @Override
   public int saveTag(SaleTag saleTag) {
      String uuid = Utils.getUUID();
      saleTag.setTagUuids(uuid);
      saleTag.setTagStatus("1");
      return super.save(saleTag);
   }

   /**切换分组*/
   @Override
   public JSONObject chooseGroup(String uuid) {
      Example example = new Example(SaleTag.class);
      example.createCriteria().andCondition("tag_group_id=",uuid).andEqualTo("tagStatus","1");
      SaleTag saleTag = this.selectByKey(uuid);
      List<SaleTag> list = this.selectByExample(example);
     JSONObject json  = new JSONObject();
     json.put("isselect",saleTag.getTagSelect());
     json.put("list",list);
      return json;
   }

   /**删除分组*/
   @Override
   public int deleteGroup(SaleTag sale) {
      Example example = new Example(SaleTag.class);
      example.createCriteria().andCondition("tag_group_id=",sale.getTagUuid()).andEqualTo("tagStatus","1");
      List<SaleTag> list = this.selectByExample(example);
      if(list.size()>0){
         return 0;
      }else{
         int num = saleTagDao.deleteGroup(sale.getTagUuid());
         return num;
      }
   }

   /**查询分组标签**/
   @Override
   public JSONArray getAllTag() {
      JSONArray jsonArray  = new JSONArray();
      try {
         Example example = new Example(SaleTag.class);
         example.createCriteria().andEqualTo("tagGroupId","0").andEqualTo("tagStatus","1");
         List<SaleTag> list = this.selectByExample(example);


         Example example1 = new Example(SaleTag.class);
         example1.createCriteria().andNotEqualTo("tagGroupId","0").andEqualTo("tagStatus","1");
         List<SaleTag> chilede = this.selectByExample(example1);
         list.forEach(saleTag -> {
            List<SaleTag> chile = new ArrayList<>();
            for (SaleTag tag:chilede){
               if (tag.getTagGroupId().equals(saleTag.getTagUuid())){
                  chile.add(tag);
               }
            }
            JSONObject json = new JSONObject(true);
            json.put("parent",saleTag);
            json.put("children",chile);
            jsonArray.add(json);
         });
      }catch (Exception e){
         log.error("获取标签失败："+e.getMessage());
      }
      return jsonArray;
   }

   /**删除标签，更新状态2*/
   @Override
   public int updateSaleTag(SaleTag tag){
      try{
         String uuid = tag.getTagUuid();
         int num = saleTagDao.updateById(uuid);
         return num;
      }catch (Exception e){
         e.printStackTrace();
         log.info(e.getMessage());
         return 0;
      }
   }
}
